home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / lib / php / Mail.php < prev    next >
Encoding:
PHP Script  |  2001-03-06  |  6.9 KB  |  175 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Chuck Hagenbuch <chuck@horde.org>                           |
  17. // +----------------------------------------------------------------------+
  18.  
  19. require_once 'PEAR.php';
  20.  
  21. /**
  22.  * PEAR's Mail:: interface. Defines the interface for implementing
  23.  * mailers under the PEAR hierarchy, and provides supporting functions
  24.  * useful in multiple mailer backends.
  25.  */
  26. class Mail extends PEAR {
  27.     
  28.     /**
  29.      * Provides an interface for generating Mail:: objects of various
  30.      * types
  31.      *
  32.      * @param string The kind of Mail:: object to instantiate.
  33.      * @param array  The parameters to pass to the Mail:: object.
  34.      * @access public
  35.      */
  36.     function factory($mailer_type, $params = array())
  37.     {
  38.         $mailer_type = strtolower($mailer_type);
  39.         $classfile = PEAR_INSTALL_DIR . '/Mail/' . $mailer_type . '.php';
  40.         if (@is_readable($classfile)) {
  41.             include_once $classfile;
  42.             $class = 'Mail_' . $mailer_type;
  43.             return new $class($params);
  44.         } else {
  45.             return new PEAR_Error('unable to find classfile: ' . $classfile);
  46.         }
  47.     }
  48.     
  49.     /**
  50.      * Implements Mail::send() function using php's built-in mail()
  51.      * command.
  52.      * 
  53.      * @param mixed Either a comma-seperated list of recipients
  54.      *              (RFC822 compliant), or an array of recipients,
  55.      *              each RFC822 valid. This may contain recipients not
  56.      *              specified in the headers, for Bcc:, resending
  57.      *              messages, etc.
  58.      *
  59.      * @param array The array of headers to send with the mail, in an
  60.      *              associative array, where the array key is the
  61.      *              header name (ie, 'Subject'), and the array value
  62.      *              is the header value (ie, 'test'). The header
  63.      *              produced from those values would be 'Subject:
  64.      *              test'.
  65.      *
  66.      * @param string The full text of the message body, including any
  67.      *               Mime parts, etc.
  68.      *
  69.      * @return mixed Returns true on success, or a PEAR_Error
  70.      *               containing a descriptive error message on
  71.      *               failure.
  72.      * @access public
  73.      */    
  74.     function send($recipients, $headers, $body)
  75.     {
  76.         // if we're passed an array of recipients, implode it.
  77.         if (is_array($recipients)) {
  78.             $recipients = implode(', ', $recipients);
  79.         }
  80.         
  81.         // get the Subject out of the headers array so that we can
  82.         // pass it as a seperate argument to mail().
  83.         $subject = '';
  84.         if (isset($headers['Subject'])) {
  85.             $subject = $headers['Subject'];
  86.             unset($headers['Subject']);
  87.         }
  88.         
  89.         // flatten the headers out.
  90.         list(,$text_headers) = Mail::prepareHeaders($headers);
  91.         
  92.         return mail($recipients, $subject, $body, $text_headers);
  93.     }
  94.     
  95.     /**
  96.      * Take an array of mail headers and return a string containing
  97.      * text usable in sending a message.
  98.      *
  99.      * @param array The array of headers to prepare, in an associative
  100.      *              array, where the array key is the header name (ie,
  101.      *              'Subject'), and the array value is the header
  102.      *              value (ie, 'test'). The header produced from those
  103.      *              values would be 'Subject: test'.
  104.      *
  105.      * @return mixed Returns false if it encounters a bad address,
  106.      *               otherwise returns an array containing two
  107.      *               elements: Any From: address found in the headers,
  108.      *               and the plain text version of the headers.
  109.      * @access protected
  110.      */
  111.     function prepareHeaders($headers)
  112.     {
  113.         // Look out for the From: value to use along the way.
  114.         $text_headers = '';  // text representation of headers
  115.         $from = null;
  116.         
  117.         foreach ($headers as $key => $val) {
  118.             if ($key == 'From') {
  119.                 $from_arr = imap_rfc822_parse_adrlist($val, 'localhost');
  120.                 $from = $from_arr[0]->mailbox . '@' . $from_arr[0]->host;
  121.                 if (strstr($from, ' ')) {
  122.                     // Reject outright envelope From addresses with spaces.
  123.                     return false;
  124.                 }
  125.                 $text_headers .= $key . ': ' . $val . "\n";
  126.             } else if ($key == 'Received') {
  127.                 // put Received: headers at the top, since Receieved:
  128.                 // after Subject: in the header order is somtimes used
  129.                 // as a spam trap.
  130.                 $text_headers = $key . ': ' . $val . "\n" . $text_headers;
  131.             } else {
  132.                 $text_headers .= $key . ': ' . $val . "\n";
  133.             }
  134.         }
  135.         
  136.         return array($from, $text_headers);
  137.     }
  138.     
  139.     /**
  140.      * Take a set of recipients and parse them, returning an array of
  141.      * bare addresses (forward paths) that can be passed to sendmail
  142.      * or an smtp server with the rcpt to: command.
  143.      *
  144.      * @param mixed Either a comma-seperated list of recipients
  145.      *              (RFC822 compliant), or an array of recipients,
  146.      *              each RFC822 valid.
  147.      *
  148.      * @return array An array of forward paths (bare addresses).
  149.      * @access protected
  150.      */
  151.     function parseRecipients($recipients)
  152.     {
  153.         // if we're passed an array, assume addresses are valid and
  154.         // implode them before parsing.
  155.         if (is_array($recipients)) {
  156.             $recipients = implode(', ', $recipients);
  157.         }
  158.         
  159.         // Parse recipients, leaving out all personal info. This is
  160.         // for smtp recipients, etc. All relevant personal information
  161.         // should already be in the headers.
  162.         $addresses = imap_rfc822_parse_adrlist($recipients, 'localhost');
  163.         $recipients = array();
  164.         if (is_array($addresses)) {
  165.             foreach ($addresses as $ob) {
  166.                 $recipients[] = $ob->mailbox . '@' . $ob->host . ' ';
  167.             }
  168.         }
  169.         
  170.         return $recipients;
  171.     }
  172.     
  173. }
  174. ?>
  175.